﻿using System;
using System.Globalization;
using System.Text;
using System.Runtime.InteropServices;

namespace BMproductions.Media.MediaPlayer.WMPPlus {
	[Flags]
	public enum ImportLibrary: int {
		Kernel32 = 0x1,
		User32 = 0x2,
		MSVCRT = 0x8,
		ApiLibraryLoader = 0x40,
		OLE32 = 0x80,
		MF = 0x100,
	};

	public class WMPPlusHookData {
		public bool Hooked { get; private set; }
		public IntPtr ModuleHandle { get; private set; }
		public IntPtr InstanceHandle { get; private set; }
		public Version Version { get; private set; }
		public ImportLibrary Libraries { get; private set; }

		public IntPtr MainMenuHandle {
			get { return WMPPlusCore.GetPtrProp(WMPPlusProperty.HookMenu); }
		}
		public bool WMPlayerClosing {
			get { return WMPPlusCore.GetBoolProp(WMPPlusProperty.HookWMPClose); }
		}

		public WMPPlusHookData() {
			Hooked = false;
			Version = new Version();
			//MainMenuHandle = IntPtr.Zero;
		}

		[return: MarshalAs(UnmanagedType.Bool)]
		[DllImport(WMPPlusPublic.WMPPlusHookName, CharSet = CharSet.Auto, EntryPoint = "#201")]
		private static extern bool ResolveLabel(
			[In, Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder label,
			int clabel,
			[MarshalAs(UnmanagedType.LPTStr)] string lib,
			[Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder reslib, int creslib);

		public static bool ResolveLabel(ref string label, string lib, out string reslib) {
			StringBuilder sb1 = new StringBuilder(1024);
			sb1.Append(label);
			StringBuilder sb2 = new StringBuilder(260);
			bool succeeded = ResolveLabel(sb1, 1024, lib, sb2, 260);
			label = sb1.ToString();
			reslib = sb2.Length == 0 ? null : sb2.ToString();
			return succeeded;
		}

		internal void Initialize3(IntPtr module, IntPtr instance, int majorVersion, int minorVersion, ImportLibrary lib) {
			Hooked = true;
			ModuleHandle = module;
			InstanceHandle = instance;
			Version = new Version(majorVersion, minorVersion);
			Libraries = lib;
			//TODO: if (module!=IntPtr.Zero && module.FileVersion.MajorVersion >= 12) TagEditorForm.DisableWMPTagPlusMPEG4();
		}
	}
}
